package com.test.baseGroovy
/**
 * ClosureTest--GroovyTest
 *
 * @author: Jovi
 *
 * @createTime: 2018-06-12 16:49
 *
 * */
aClosure = {
        //闭包是一段代码，所以需要花括号括起来

    String param1, int param2 ->//这个箭头很关键，箭头前面是参数定义，后面是代码
        println 'this is code'
}

aClosure.call("nihao", 100)


fun1 = {
    p1 ->
        s = "我是一个闭包，$p1"
}
println fun1.call()
println fun1.call("我是参数")
println(fun1("我是p1参数"))


fun2 = {
    it -> "aaaa$it"
}
println fun2(1)

fun3 = {
    -> "bbbb" //如果在闭包定义时，采用下面这种写法，则表示闭包没有参数！
}
println fun3()
//println fun3(1)//执行这个方法的时候就会报错


list = [1, 2, 3]
list.each { //当函数的最后一个参数是闭包的话，可以省略圆括号
    println it
}

def fun (int a1, Closure clo) {
    clo("haha")
}

println fun(1,{aa -> "nihao$aa"})

map = ["TopicName": "Maps", "TopicDescription": "Methods in Maps"]
map.each { println it }
map.each { println it.key+" maps to:"+it.value }

list.each {
    num ->
        if (num % 2 == 0) {
            println num
        }
}
